package top.infopub.vo;


import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import top.infopub.common.ErrorCodeEnum;


/**
 * ResponseVo
 * @author Awoke
 * @version 2018年9月13日
 * @see ResponseVo
 * @since
 */
public class ResponseVo<T> implements Serializable {

    private static final long serialVersionUID = -5257371041266762944L;

    /**
     * 是否成功标志。
     * 需要特别说明的是，如果返回处理中，则此处为false
     */
    private boolean isSuccess;

    /**
     * 错误编码。参考{@link ErrorCodeEnum}
     */
    private int errorCode;

    /**
     * 错误返回说明
     */
    private String msg;

    /**
     * 返回的实体类
     */
    private T data;

    /**
     * 记录总条数
     */
    private long total = 0;

    /**
     * 结果集存放List
     */
    private List<T> rows = new LinkedList<>();

    /**
     * isSuccess=true
     * @param data
     */
    public ResponseVo(T data) {
        super();
        this.isSuccess = true;
        this.errorCode = 0;
        this.data = data;
    }

    /**
     * isSuccess=true
     * @param total
     * @param data
     */
    public ResponseVo(long total, T data) {
        super();
        this.isSuccess = true;
        this.errorCode = 0;
        this.data = data;
        this.total = total;
    }

    /**
     * isSuccess=true
     * @param total
     * @param rows
     */
    public ResponseVo(long total, List<T> rows) {
        super();
        this.isSuccess = true;
        this.errorCode = 0;
        this.total = total;
        this.rows = rows;
    }

    public ResponseVo(boolean isSuccess, int errorCode, String msg) {
        super();
        this.isSuccess = isSuccess;
        this.errorCode = errorCode;
        this.msg = msg;
    }

    public ResponseVo(boolean isSuccess, int errorCode, String msg, T data) {
        super();
        this.isSuccess = isSuccess;
        this.errorCode = errorCode;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseVo<T> getSuccessResponse() {
        return new ResponseVo<T>(true, 0, null);
    }

    public static <T> ResponseVo<T> getSuccessResponse(T data) {
        return new ResponseVo<T>(data);
    }
    
    public static <T> ResponseVo<T> getSuccessResponse(int errorCode, String msg, T data) {
    	return new ResponseVo<T>(true, errorCode, msg, data);
    }

    /** Success 同时设置msg **/
    public static <T> ResponseVo<T> getSuccessMsgResponse(String msg) {
        ResponseVo<T> rsp = new ResponseVo<T>(null);
        rsp.setMsg(msg);
        return rsp;
    }

    public static <T> ResponseVo<T> getSuccessResponse(long total, T data) {
        return new ResponseVo<T>(total, data);
    }

    public static <T> ResponseVo<T> getSuccessResponse(long total, List<T> rows) {
        return new ResponseVo<T>(total, rows);
    }
    
    public static <T> ResponseVo<T> getFailureResponse(Integer code, String msg) {
    	return new ResponseVo<T>(false, code, msg);
    }

    public ResponseVo<T> setDataObj(T data) {
        this.setData(data);
        return this;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
